package com.zhuiYun.project.api.customer.mapper;


import cn.hutool.json.JSONObject;
import com.zhuiYun.project.api.login.entity.User;
import com.zhuiYun.project.common.mapper.BaseMapper;
import com.zhuiYun.project.api.customer.entity.FriendEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * LC: 2023-04-03 14:10
 */
@Mapper
public interface FriendMapper extends BaseMapper<FriendEntity> {

    /**
     * 查询会话列表
     * LC: 2023-04-04 14:48
     * @param myId
     * @param friendId
     * @return
     */
    @Select("SELECT * FROM friend where my_id = #{myId} and friend_id = #{friendId} and del_flag = 0")
    FriendEntity getFriend(@Param("myId") Integer myId, @Param("friendId") Integer friendId);

    /**
     * 修改会话列表
     * @param sessionId
     * @param friendId
     * @param lastMessage
     * @param UpdateTime
     * @param unread
     */
    @Update("UPDATE friend SET last_content = #{lastMessage}, unread = #{unread},update_time = #{UpdateTime} WHERE session_id = #{sessionId} and friend_id = #{friendId} and del_flag = 0")
    void updateFriendByIds(@Param("sessionId") Long sessionId, @Param("friendId") Integer friendId, @Param("lastMessage") String lastMessage, @Param("UpdateTime") Date UpdateTime, @Param("unread") Integer unread);


    /**
     * @descriptions 获取客服信息
     * LC: 2023-04-04 13:52
     * @param myId
     * @return result.CommonResult
     */
    @Select("SELECT f.id,f.session_id,f.friend_id,f.last_content,f.unread,f.update_time,f.remark,u.avatar_url " +
            "FROM friend f,user u " +
            "where u.id=f.friend_id and f.del_flag=0 and f.my_id=#{myId} ORDER BY f.update_time desc")
    List<JSONObject> getFriendList(Integer myId);

    /**
     * 清空读取信息
     * LC: 2023-04-04 13:53
     * @param myId
     * @param friendId
     * @return
     */
    @Update("UPDATE friend SET unread = 0 WHERE  my_id = #{myId} and friend_id = #{friendId} and del_flag = 0")
    void emptyUnRead(@Param("myId") Integer myId,@Param("friendId") Integer friendId);

    /**
     * 根据登录用户id查询会话
     * LC: 2023-04-04 15:02
     * @param id
     * @return
     */
    @Select("SELECT * FROM friend where my_id = #{id} ")
    List<FriendEntity> getUserFriend(Integer id);

    /**
     * 根据登录用户id查询用户表
     * LC: 2023-04-04 15:02
     * @param id
     * @return
     */
    @Select("SELECT * FROM user where id = #{id}")
    User getUserList(Integer id);
}
